#include "logger.h"

void Logger::warn(const char * file, int line, const char * func, const char * msg){
    //std::cout<<file<<line<<func<<msg<<std::endl;
    _root->warn("%s %d %s %s",file, line, func, msg);
}
void Logger::error(const char * file, int line, const char * func, const char * msg){
    _root->error("%s %d %s %s",file, line, func, msg);

}
void Logger::debug(const char * file, int line, const char * func, const char * msg){
    _root->debug("%s %d %s %s",file, line, func, msg);

}
void Logger::info(const char * file, int line, const char * func, const char * msg){
    _root->info("%s %d %s %s",file, line, func, msg);

}
Logger *Logger::getInstance(){
    if(!_pInstance){
        _root = &Category::getRoot();
        _pInstance = new Logger();


    }

    return _pInstance;
}

void Logger::destroy(){
    //TODO 销毁单例
    delete _pInstance;
    _pInstance = nullptr;
    _root = nullptr;
}

Logger::Logger(){
    PatternLayout *ptn_pos = new PatternLayout();
    PatternLayout *ptn_roll = new PatternLayout();
    ptn_pos->setConversionPattern("%d %c [%p] %m%n");
    ptn_roll->setConversionPattern("%d %c [%p] %m%n");

    OstreamAppender *pos = new OstreamAppender("iosout", &cout);
    RollingFileAppender *rfPos = new RollingFileAppender("rolout", "wd.log");
    pos->setLayout(ptn_pos);
    rfPos->setLayout(ptn_roll);

    _root->setPriority(Priority::DEBUG);
    _root->addAppender(pos);
    _root->addAppender(rfPos);
}

Logger::~Logger(){
    //Category::shutdown();
}

Logger *Logger::_pInstance = nullptr;
Category *Logger::_root = nullptr;

